package cn.signalpha.modules.shop.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.dto.IdDTO;
import cn.signalpha.common.enums.StatusEnum;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.excel.util.ExcelUtil;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.DateUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.StringPool;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.excel.UserPlaceExcel;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.com.vo.UserPlaceVO;
import cn.signalpha.modules.shop.dto.PlaceDTO;
import cn.signalpha.modules.shop.service.OrderSettlePlaceService;
import cn.signalpha.modules.shop.vo.OrderSettlePlaceVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/place")
@Api(value = "分销员", tags = "分销员")
public class PlaceController extends BaseController {

	private final UserService userService;
	private final OrderSettlePlaceService orderSettlePlaceService;

    @GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="parentId", dataType = "string", value = "直推人id"),
		@ApiImplicitParam(name="grandpaId", dataType = "string", value = "间推人id"),
	})
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<UserPlaceVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("isPlace", "1");
        return R.data(userService.selectUserPlacePage(Condition.getPage(query), params));
    }

	@GetMapping("/export")
	@ApiOperation(value = "导出excel", notes = "")
	public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		List<UserPlaceExcel> list = userService.selectUserPlaceExcel(params);
		ExcelUtil.export(response, DateUtil.time(), StringPool.EMPTY, list, UserPlaceExcel.class);
	}

	@PostMapping("/freeze")
	@ApiOperation(value = "冻结分销员", notes = "传入userId")
	public R freeze(@Valid @RequestBody IdDTO idDTO) {
		User user = userService.getById(idDTO.getId());
		Assert.isNull(user, MsgConstant.OBJECT_NULL);
		Assert.isTrue(user.getPlaceStatus().equals(StatusEnum.ON.getType()), "当前分销员状态不可冻结");
		userService.updateById(User.builder().id(user.getId()).placeStatus(StatusEnum.OFF.getType()).build());
		return R.status(true);
	}

	@PostMapping("/freeze/remove")
	@ApiOperation(value = "解冻分销员", notes = "传入userId")
	public R freezeRemove(@Valid @RequestBody IdDTO idDTO) {
		User user = userService.getById(idDTO.getId());
		Assert.isNull(user, MsgConstant.OBJECT_NULL);
		Assert.isTrue(user.getPlaceStatus().equals(StatusEnum.OFF.getType()), "当前分销员状态不可解冻");
		userService.updateById(User.builder().id(user.getId()).placeStatus(StatusEnum.ON.getType()).build());
		return R.status(true);
	}

	@PostMapping("/update/batch/level")
	@ApiOperation(value = "批量更新等级", notes = "")
	public R updateBatchLevel(@Valid @RequestBody PlaceDTO.UpdateLevelDTO updateDTO) {
		return R.status(userService.update(Wrappers.<User>lambdaUpdate().set(User::getPlaceLevel, updateDTO.getLevel()).in(User::getId, Func.toLongList(updateDTO.getId()))));
	}

	@GetMapping("/order/settle/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="userId", dataType = "string", value = "用户id"),
		@ApiImplicitParam(name="settleType", dataType = "string", value = "结算类型：1直推 2间推"),
		@ApiImplicitParam(name="settleStatus", dataType = "string", value = "结算状态：0未结算 1已结算"),
		@ApiImplicitParam(name="createTimeBegin", dataType = "string", value = "开始时间"),
		@ApiImplicitParam(name="createTimeEnd", dataType = "string", value = "结束时间"),
	})
	@ApiOperation(value = "佣金记录和推广人订单", notes = "")
	public R<IPage<OrderSettlePlaceVO>> orderSettlePage(@ApiIgnore @RequestParam Map<String, Object> params, Query query){
		return R.data(orderSettlePlaceService.selectOrderSettlePlacePage(Condition.getPage(query), params));
	}

}
